﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CaptchaSample
{
    public partial class CaptchaValidator : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            initializeCallback();
        }

        private void initializeCallback()
        {
            ClientScriptManager cm = Page.ClientScript;
            String cbReference = cm.GetCallbackEventReference(this, "arg",
                "ReceiveServerData", "");
            String callbackScript = "function CallServer(arg, context) {" +
                cbReference + "; }";
            cm.RegisterClientScriptBlock(this.GetType(),
                "CallServer", callbackScript, true);
        }



        public string GetCallbackResult()
        {
            return CaptchaControl1.UserValidated.ToString().ToLower();
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            if (isValidValidationArg(eventArgument))
            {
                CaptchaControl1.ValidateCaptcha(eventArgument);
            }
        }

        private static bool isValidValidationArg(string eventArgument)
        {
            return !string.IsNullOrEmpty(eventArgument);
        }
    }
}